﻿/*!
 * Copyright (c), 上海沃恩信息科技有限公司.
 * All rights reserved.
 * Licensed under BSD (https://www.pittypat.work/bsd.html).
 */

document.addEventListener('DOMContentLoaded', function () {
    'use strict';

    function Query(items) {
        var me = this;
        
        for (var i = 0; i < items.length; ++i) {
            me[i] = items[i];
        }
        me.length = items.length;
    }

    Query.prototype.on = function (type, listener) {
        var me = this;
        for (var i = 0; i < me.length; ++i) {
            me[i].addEventListener(type, listener);
        }

        return me;
    };

    Query.prototype.each = function (cb) {
        var me = this;
        for (var i = 0, item = null; i < me.length; ++i) {
            item = me[i];
            cb.call(item, i, item);
        }

        return me;
    };

    Query.prototype.attr = function (name) {
        var me = this;
        return me.length ? me[0].getAttribute(name) : '';
    };

    var $ = function (selector) {
        return new Query(typeof selector === 'string' ? document.querySelectorAll(selector) : [selector]);
    };

    (function () {
        // 返回功能页面
        $('.pt-help-back').on('click', function () {
            var $me = $(this),
                href = $me.attr('data-href');

            if (!href) {
                history.back();
            } else {
                var url = window.location;
                window.location = href + (url.search || '') + (url.hash || '');
            }
        });

        // 打印
        $('.pt-help-print').on('click', function () {
            window.print();
        });

        // 检查帮助截图是否加载成功，如果没有成功加载图片，则显示默认的占位图片
        function imgOk(img) {
            if (!img.complete) {
                return false;
            }

            if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
                return false;
            }

            return true;
        }

        function appPath() {
            try {
                return localStorage.getItem('__APP_PATH') || '';
            } catch (e) {
                console.log(e);
                return '';
            }
        }

        // 获取指定路径（使用 '~/' 开头的路径）的完整的绝对路径
        function mapUrl(url) {
            if (!url || typeof url != 'string') {
                return '';
            }

            if (url.charAt(0) != '~') {
                return url;
            }

            if (url.length > 0) {
                if (url.charAt(1) == '/' || url.charAt(1) == '\\') {
                    url = url.substr(2); // 去掉 '~/'
                } else {
                    url = url.substr(1); // 去掉 '~'
                }
            } else {
                url = '';
            }

            return appPath() + '/' + url;
        }

        var phUrl = mapUrl('~/help/images/placeholder.png');
        $(window).on('load', function () {
            $('.help-img>img').each(function (i, img) {
                if (!imgOk(img)) {
                    img.src = phUrl;
                }
            });
        });

        if (window.location.hash) {
            var e = document.querySelector(window.location.hash);
            if (e) {
                e.scrollIntoView();
            }
        }
    })();

    // 初始化 mdc
    mdc.autoInit();
});
